// Copyright (c) 2006-2007 Zorm
// Released under the MIT License

#include "PCH.h"
#include "Common.h"

RadarSites::RadarSites() {

	std::ifstream in("sites.xml");
    in.unsetf(std::ios::skipws);
                
    std::vector<char> buf;
    std::copy(std::istream_iterator<char>(in), std::istream_iterator<char>(), std::back_inserter(buf));
    buf.push_back(0);
    
    xml_parser parser(&buf[0], pugi::parse_minimal);

    xml_node doc = parser.document();
        
    if (xml_node sites = doc.first_element("sites")) {
        for (xml_node location = sites.first_element("location"); location; location = location.next_sibling()) {
			RadarState *state = new RadarState;
			state->stateName = location.attribute("place").value();
			//ATH::Instance()->GetLog()->LogEvent(Log::LOG_INFO, TEXT("Site %s.\n"), location.attribute("place").value());
			for (xml_node radar = location.first_element("radar"); radar; radar = radar.next_sibling()) {
				RadarSite *site = new RadarSite;
				site->siteID = radar.attribute("site").value();
				site->desc = radar.attribute("desc").value();
				state->sites.push_back(site);
				//ATH::Instance()->GetLog()->LogEvent(Log::LOG_INFO, TEXT("Radar %s at %s.\n"), radar.attribute("site").value(), radar.attribute("desc").value());
			}
			m_states.push_back(state);
        }
    }
}

void RadarSites::FillStates(HWND statesCombo) {
	for (std::vector<RadarState*>::const_iterator iter = m_states.begin(); iter != m_states.end(); ++iter) {
		RadarState *state = (*iter);
		SendMessage(statesCombo, CB_ADDSTRING, NULL, (LPARAM)state->stateName.c_str());
	}
}

void RadarSites::FillSites(HWND sitesCombo, int index) {
	RadarState *state = m_states[index];
	SendMessage(sitesCombo, CB_RESETCONTENT, NULL, NULL);
	for (std::vector<RadarSite*>::const_iterator iter = state->sites.begin(); iter != state->sites.end(); ++iter) {
		RadarSite *site = (*iter);
		SendMessage(sitesCombo, CB_ADDSTRING, NULL, (LPARAM)site->desc.c_str());
	}
}

void RadarSites::SetupRadarSite(int stateIndex, int siteIndex, int productIndex) {
	RadarState *state = m_states[stateIndex];
	RadarSite *site = state->sites[siteIndex];
	ATH::Instance()->GetRadarManager()->CreateRadar(site->siteID, std::string("DS.1"), site->desc, std::string("Reflectivity 1"));
	ATH::Instance()->GetLog()->LogEvent(Log::LOG_INFO, TEXT("%s, %s (%s)\n"), site->desc.c_str(), state->stateName.c_str(), site->siteID.c_str());
}